package org.dce.screencapturer.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import org.dce.screencapturer.Drawing;

@SuppressWarnings("serial")
public class SlideView extends JComponent {

	private JScrollPane scrolledArea = new JScrollPane();
	private JPanel contents = new JPanel(new FlowLayout(FlowLayout.LEFT));
	private List<SlideViewListener> listeners = new ArrayList<SlideViewListener>();
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private final ActionListener listener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			selectSlide((DrawingView)e.getSource());
		}
	};
	
	public SlideView(){
		setPreferredSize(new Dimension(300,84));
		setLayout(new BorderLayout());
		add(scrolledArea,BorderLayout.CENTER);
		scrolledArea.setViewportView(contents);
		scrolledArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
	}
	
	public void selectDrawing(Drawing drawing) {
		getSlideForDrawing(drawing).setSelected(true);
	}
	
	private DrawingView getSlideForDrawing(Drawing drawing){
		for (Component c:contents.getComponents()){
			DrawingView slideView = (DrawingView)c;
			if (slideView.getDrawing() == drawing){
				return slideView;
			}
		}
		return null;
	}
	
	protected void selectSlide(DrawingView source) {
		notifyDrawingSelected(source.getDrawing());
	}

	public void addDrawing(Drawing drawing){
		DrawingView drawingView = new DrawingView(drawing);
		buttonGroup.add(drawingView);
		drawingView.addActionListener(listener);
		contents.add(drawingView);
		contents.invalidate();
		notifyDrawingAdded(drawing);
	}
	
	public void addSlideViewListener(SlideViewListener listener){
		listeners.add(listener);
	}
	
	public void removeSlideViewListener(SlideViewListener listener){
		listeners.remove(listener);
	}
	
	protected void notifyDrawingSelected(Drawing drawing){
		SlideViewEvent event = new SlideViewEvent(this, drawing);
		for (SlideViewListener listener:listeners){
			listener.drawingSelected(event);
		}
	}
	
	protected void notifyDrawingAdded(Drawing drawing){
		SlideViewEvent event = new SlideViewEvent(this, drawing);
		for (SlideViewListener listener:listeners){
			listener.drawingAdded(event);
		}
	}
	
	protected void notifyDrawingRemoved(Drawing drawing){
		SlideViewEvent event = new SlideViewEvent(this, drawing);
		for (SlideViewListener listener:listeners){
			listener.drawingRemoved(event);
		}
	}
	
	private class DrawingView extends JRadioButton{
		private Drawing drawing;
		
		private BufferedImage image;
		
		public DrawingView(Drawing drawing){
			this.drawing = drawing;
			
			AffineTransform tx = new AffineTransform();
			
			double scale =  64.0 / drawing.getHeight();
			
			int width = (int)(drawing.getWidth() * scale);

			image = new BufferedImage(width, 64, BufferedImage.TYPE_INT_RGB);

			Graphics2D g2d = image.createGraphics();
			
			tx.scale(scale,scale);
			
			g2d.setTransform(tx);
			
			drawing.paint(g2d);
			
			setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
			setBorder(BorderFactory.createLineBorder(Color.BLACK));
			setMargin(new Insets(0,0,0,0));
			setContentAreaFilled(false);
		}
		
		public Drawing getDrawing() {
			return drawing;
		}
		
		protected void paintComponent(Graphics g){
			g.drawImage(image,0,0,this);
			
			if (isSelected()){
				g.setColor(Color.RED);
				g.drawRect(0,0,getWidth()-1,getHeight()-1);
			}
			
		}

	}

}
